{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e752b05e",
   "metadata": {},
   "source": [
    "## 文档\n",
    "\n",
    "- 2.x https://aioredis.readthedocs.io/en/latest/getting-started/\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a78488bb",
   "metadata": {},
   "source": [
    "## 基础用法\n",
    "\n",
    "参考文档： https://github.com/aio-libs/aioredis-py/tree/master/docs/examples/getting_started\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24937c25",
   "metadata": {},
   "outputs": [],
   "source": [
    "import asyncio\n",
    "import aioredis \n",
    "import aiofiles\n",
    "import json\n",
    "from io import StringIO\n",
    "\n",
    "async def readJsonCfg(filePath):\n",
    "    async with aiofiles.open(filePath, mode=\"r\") as f:\n",
    "        contents = await f.read()\n",
    "        io = StringIO(contents)\n",
    "        cfgDir = json.load(io)\n",
    "        return cfgDir\n",
    "\n",
    "async def executeRedis(address, password):\n",
    "    redis = await aioredis.from_url(address, password=password, decode_responses=True)\n",
    "    await redis.set(\"name\", '李磊123456')\n",
    "    result = await redis.get('name')\n",
    "    print(result)\n",
    "    await redis.close()\n",
    "    print('end')\n",
    "\n",
    "async def main():\n",
    "    redisCfg = await readJsonCfg(\"E:\\\\projs\\\\my\\\\rediscfg.json\")\n",
    "    await executeRedis( 'redis://{0}:{1}'.format(redisCfg['host'], str(redisCfg['port'])), redisCfg['passwd'])\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    await main()    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7c701c8",
   "metadata": {},
   "source": [
    "## 连接池"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "aefdc7d6",
   "metadata": {},
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "asyncio.run() cannot be called from a running event loop",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "Input \u001b[1;32mIn [1]\u001b[0m, in \u001b[0;36m<cell line: 29>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     27\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraw value:\u001b[39m\u001b[38;5;124m\"\u001b[39m, val)\n\u001b[0;32m     29\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m---> 30\u001b[0m     asyncio\u001b[38;5;241m.\u001b[39mrun(main())\n\u001b[0;32m     31\u001b[0m     asyncio\u001b[38;5;241m.\u001b[39mrun(main_pool())\n",
      "File \u001b[1;32mD:\\apps\\Python39\\lib\\asyncio\\runners.py:33\u001b[0m, in \u001b[0;36mrun\u001b[1;34m(main, debug)\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[38;5;124;03m\"\"\"Execute the coroutine and return the result.\u001b[39;00m\n\u001b[0;32m     10\u001b[0m \n\u001b[0;32m     11\u001b[0m \u001b[38;5;124;03mThis function runs the passed coroutine, taking care of\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m     30\u001b[0m \u001b[38;5;124;03m    asyncio.run(main())\u001b[39;00m\n\u001b[0;32m     31\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m     32\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m events\u001b[38;5;241m.\u001b[39m_get_running_loop() \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m---> 33\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\n\u001b[0;32m     34\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124masyncio.run() cannot be called from a running event loop\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m     36\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m coroutines\u001b[38;5;241m.\u001b[39miscoroutine(main):\n\u001b[0;32m     37\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124ma coroutine was expected, got \u001b[39m\u001b[38;5;132;01m{!r}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mformat(main))\n",
      "\u001b[1;31mRuntimeError\u001b[0m: asyncio.run() cannot be called from a running event loop"
     ]
    }
   ],
   "source": [
    "import asyncio\n",
    "import aioredis\n",
    "import aiofiles\n",
    "import json\n",
    "from io import StringIO\n",
    "\n",
    "async def readJsonCfg(filePath):\n",
    "    async with aiofiles.open(filePath, mode=\"r\") as f:\n",
    "        contents = await f.read()\n",
    "        io = StringIO(contents)\n",
    "        cfgDir = json.load(io)\n",
    "        return cfgDir\n",
    "\n",
    "async def main():\n",
    "    redisCfg = await readJsonCfg(\"E:\\\\projs\\\\my\\\\rediscfg.json\")\n",
    "    redis = aioredis.from_url(\"redis://{0}:{1}/3\".format(redisCfg['host'], redisCfg['port']), password=redisCfg['passwd'], max_connections=10)\n",
    "    await redis.execute_command(\"set\", \"name1\", \"aaa\")\n",
    "    val = await redis.execute_command(\"get\", \"name1\")\n",
    "    print(\"raw value:\", val)\n",
    "\n",
    "async def main_pool():\n",
    "    redisCfg = await readJsonCfg(\"E:\\\\projs\\\\my\\\\rediscfg.json\")\n",
    "    pool = aioredis.ConnectionPool.from_url(\"redis://{0}:{1}/3\".format(redisCfg['host'], redisCfg['port']), password=redisCfg['passwd'], max_connections=10)\n",
    "    redis = aioredis.Redis(connection_pool=pool)\n",
    "    await redis.execute_command(\"set\", \"name2\", \"bbb\")\n",
    "    val = await redis.execute_command(\"get\", \"name2\")\n",
    "    print(\"raw value:\", val)\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    asyncio.run(main())\n",
    "    asyncio.run(main_pool())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e43c98b9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
